package com.example.testclient;

import com.example.testservice.IService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class MainActivity extends Activity {
	private IService iService = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("MyService", "MyService onCreate");
		bindService(new Intent("com.example.testservice.server"), connection, BIND_AUTO_CREATE);
		Log.d("MyService", "MyService onCreate222222");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindService(connection);
	}

	private ServiceConnection connection = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.d("MyService", "MyService onServiceDisconnected11111");
			iService = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.d("MyService", "MyService onServiceConnected11111");
			iService = IService.Stub.asInterface(service);
			try {
				long date = iService.getCurrent();
				Log.d("MyService", "MyService date:" + date);
				iService.setValue(100);
			} catch (Exception e) {
				Log.e("MyService", "MyService date:" + e);
				e.printStackTrace();
			}
			Log.d("MyService", "MyService onServiceConnected22222");
		}
	};

}
